home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 72 / IOPROG_72.ISO / soft / Codice / CRobots / Codice Robot anni precedenti / CR2-SRC.ZIP / RANDOM.R < prev    next >
Encoding:
Text File  |  1992-09-11  |  3.2 KB  |  166 lines

  1. /*
  2.  
  3.    ROBOT         :Random.R
  4.    
  5.    PROGRAMMATORE :Mattia Rossi 
  6.  
  7.    INDIRIZZO     :Via Circonvallazione 13 - Strambino - TO
  8.  
  9.    TELEFONO      :0125 - 712421        
  10.  
  11. */
  12.  
  13.  
  14.  
  15.  
  16. /* Il robot ha un comportamento casuale al massimo: si muove a caso per tutto
  17.    il campo di battaglia cercando bersagli da colpire.
  18.    La routine di fuoco e' minimizzata al massimo per permettere un alto 
  19.    volume di fuoco                                                           */
  20.  
  21.  
  22. int    dir, direz  ;
  23.  
  24.  
  25. main()
  26. {
  27.     dir = 0;
  28.  
  29.     while ( 1 ) {
  30.  
  31.  
  32.         direz = plot_course ( rand(1000) , rand(1000) ); /* sceglie una direzione
  33.                                                                     a caso               */
  34.  
  35.         drive ( direz , 100 );                            /* vi si dirige alla 
  36.                                                                     massima velocita'    */
  37.  
  38.  
  39.         while (loc_x() > 150 && loc_y() > 150 && loc_y() < 850 && loc_x() < 850 ) {
  40.  
  41.  
  42.             drive (direz, 100);
  43.             cerca()          ;                       /* Routine di ricerca e 
  44.                                                                     di fuoco            */
  45.  
  46.         }
  47.  
  48.         drive(direz, 50);                                 /* Quando e' vicino al 
  49.                                     margine riduce la sua
  50.                                     velocita' per poter
  51.                                             girare              */
  52.  
  53.     }
  54.  
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. /* La routine di ricerca e' un semplice scan circolare del campo di battaglia
  63.    Viene eseguita un numero finito di volte e poi sospesa per poter controlla_
  64.    re la posizione del robot ed evitare che vada a sbattere                  */
  65.  
  66.  
  67.  
  68.  
  69. cerca()
  70. {
  71.     int    range , cycle , cycle1;
  72.  
  73.     cycle = 0;
  74.  
  75.  
  76.     while (cycle < 2 ) {
  77.  
  78.  
  79.         cycle1 = 0;
  80.  
  81.  
  82.         /* Cerca un Robor, Angolo di 10 gradi */
  83.  
  84.  
  85.         if (range = scan(dir, 10)) {
  86.  
  87.  
  88.             cannon(dir + 3 , 7 * range / 8 ); /* spara! ( Jazz insegna )*/
  89.  
  90.             dir  -= 3 ;
  91.  
  92.  
  93.             /* Esegue uno scan più preciso */
  94.  
  95.  
  96.             while (cycle1 < 10 ) {
  97.  
  98.  
  99.                 range = scan(dir , 3);   /* cerca in una direzione, angolo di 10 gradi */
  100.  
  101.                 if (range > 70 )         /* piu' vicino mi faccio male anch'io         */ {
  102.  
  103.  
  104.                     cannon(dir , 7 * range / 8 ); /* spara! */
  105.  
  106.                     dir  -= 8 ;
  107.  
  108.  
  109.                 }
  110.  
  111.                 dir += 5 ;
  112.                 cycle1 +=  1  ;
  113.  
  114.             }
  115.  
  116.         }
  117.         dir += 10;
  118.         cycle += 1 ;
  119.     }
  120.  
  121.  
  122. }
  123.  
  124.  
  125. /* plot course function, return degree heading to */
  126. /* reach destination x, y; uses atan() trig function */
  127. plot_course(xx, yy)
  128. int    xx, yy;
  129. {
  130.     int    d;
  131.     int    x, y;
  132.     int    scale;
  133.     int    curx, cury;
  134.  
  135.     scale = 100000;  /* scale for trig functions */
  136.     curx = loc_x();  /* get current location */
  137.     cury = loc_y();
  138.     x = curx - xx;
  139.     y = cury - yy;
  140.  
  141.     /* atan only returns -90 to +90, so figure out how to use */
  142.     /* the atan() value */
  143.  
  144.     if (x == 0) {      /* x is zero, we either move due north or south */
  145.         if (yy > cury)
  146.             d = 90;        /* north */
  147.         else
  148.             d = 270;       /* south */
  149.     } else {
  150.         if (yy < cury) {
  151.             if (xx > curx)
  152.                 d = 360 + atan((scale * y) / x);  /* south-east, quadrant 4 */
  153.             else
  154.                 d = 180 + atan((scale * y) / x);  /* south-west, quadrant 3 */
  155.         } else {
  156.             if (xx > curx)
  157.                 d = atan((scale * y) / x);        /* north-east, quadrant 1 */
  158.             else
  159.                 d = 180 + atan((scale * y) / x);  /* north-west, quadrant 2 */
  160.         }
  161.     }
  162.     return (d);
  163. }
  164.  
  165.  
  166.